# Contributor:
# Maintainer:
pkgname=lizardfs
pkgver=3.12.0
pkgrel=1
pkgdesc="a highly reliable, scalable and efficient distributed file system"
url="https://github.com/lizardfs/lizardfs/"
arch="all"
license="GPL-3.0-only"
makedepends="boost-dev cmake fuse-dev zlib-dev asciidoc python2"
pkgusers="mfs"
pkggroups="mfs"
install="$pkgname.pre-install"
subpackages="
	$pkgname-doc
	$pkgname-master
	$pkgname-metalogger
	$pkgname-chunkserver
	$pkgname-tools
	$pkgname-cgi
	$pkgname-cgiserv
	$pkgname-adm
	$pkgname-bash-completion:bashcomp:noarch
	"
source="lizardfs-$pkgver.tar.gz::https://github.com/lizardfs/lizardfs/archive/v$pkgver.tar.gz
	lizardfs-cgiserv.initd
	lizardfs-chunkserver.initd
	lizardfs-master.initd
	lizardfs-metalogger.initd
	677.patch
	"

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	cmake -B build \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/ \
		-DENABLE_TESTS=NO \
		-DENABLE_DEBIAN_PATHS=YES \
		$CMAKE_CROSSOPTS .
	make -C build
}

package() {
	make -C build DESTDIR="$pkgdir" install
	chown -R mfs:mfs "$pkgdir"/var/lib/mfs
	for i in $source; do
		case $i in
		*.initd)
			install -Dm755 "$srcdir"/$i \
				"$pkgdir"/etc/init.d/${i%.*}
			;;
		esac
	done

	mkdir -p "$pkgdir"/usr/share/bash-completion
	mv "$pkgdir"/etc/bash_completion.d "$pkgdir"/usr/share/bash-completion/completions
}

_mv_files() {
	local i
	for i in "$@"; do
		case $i in
		*/*) mkdir -p "$subpkgdir"/${i%/*};;
		*) mkdir -p "$subpkgdir";;
		esac
		mv "$pkgdir"/$i "$subpkgdir"/$i
	done
}

master() {
	pkgdesc="LizardFS master server"
	depends="lizardfs=$pkgver-r$pkgrel"
	cd "$pkgdir"
	_mv_files \
		usr/sbin/mfsmaster \
		usr/sbin/mfsrestoremaster \
		usr/sbin/mfsmetadump \
		usr/sbin/mfsmetarestore \
		etc/mfs/mfsexports.cfg.dist \
		etc/mfs/mfstopology.cfg.dist \
		etc/mfs/mfsgoals.cfg.dist \
		etc/mfs/mfsmaster.cfg.dist \
		etc/mfs/globaliolimits.cfg.dist \
		var/lib/mfs/metadata.mfs.empty \
		etc/init.d/lizardfs-master
	chown -R mfs:mfs "$subpkgdir"/var/lib/mfs
}

metalogger() {
	pkgdesc="LizardFS metadata replication server"
	depends="lizardfs=$pkgver-r$pkgrel"
	cd "$pkgdir"
	_mv_files \
		usr/sbin/mfsmetalogger \
		etc/mfs/mfsmetalogger.cfg.dist \
		etc/init.d/lizardfs-metalogger
}

chunkserver() {
	pkgdesc="LizardFS data server"
	depends="lizardfs=$pkgver-r$pkgrel"
	cd "$pkgdir"
	_mv_files \
		usr/sbin/mfschunkserver \
		etc/mfs/mfschunkserver.cfg.dist \
		etc/mfs/mfshdd.cfg.dist \
		etc/init.d/lizardfs-chunkserver
}

tools() {
	pkgdesc="LizardFS tools"
	depends="fuse"
	cd "$pkgdir"
	_mv_files \
		usr/bin/mfstools.sh \
		usr/bin/mfsmount \
		usr/bin/mfsappendchunks \
		usr/bin/mfscheckfile \
		usr/bin/mfsdeleattr \
		usr/bin/mfsdirinfo \
		usr/bin/mfsfileinfo \
		usr/bin/mfsfilerepair \
		usr/bin/mfsgeteattr \
		usr/bin/mfsgetgoal \
		usr/bin/mfsgettrashtime \
		usr/bin/mfsmakesnapshot \
		usr/bin/mfsrepquota \
		usr/bin/mfsrgetgoal \
		usr/bin/mfsrgettrashtime \
		usr/bin/mfsrsetgoal \
		usr/bin/mfsrsettrashtime \
		usr/bin/mfsseteattr \
		usr/bin/mfssetgoal \
		usr/bin/mfssetquota \
		usr/bin/mfssettrashtime \
		etc/mfs/mfsmount.cfg.dist \
		etc/mfs/iolimits.cfg.dist

}

cgi() {
	pkgdesc="LizardFS CGI Monitor"
	depends="python2"
	cd "$pkgdir"
	_mv_files \
		usr/share/mfscgi
}

cgiserv() {
	pkgdesc="Simple CGI-capable HTTP server to run LizardFS CGI Monitor"
	depends="$pkgname-cgi=$pkgver-r$pkgrel"
	cd "$pkgdir"
	_mv_files \
		usr/sbin/lizardfs-cgiserver \
		usr/sbin/mfscgiserv \
		etc/init.d/lizardfs-cgiserv
}

adm() {
	pkgdesc="LizardFS administration utility"
	depends=""
	cd "$pkgdir"
	_mv_files \
		usr/bin/lizardfs-admin \
		usr/bin/lizardfs-probe
}

bashcomp() {
	depends=""
	pkgdesc="Bash completions for $pkgname"
	install_if="$pkgname=$pkgver-r$pkgrel bash-completion"

	amove usr/share/bash-completion/completions
}

sha512sums="0136114266dfadcf8e2205bfd19f50ee201566958fba1dc97d4a238ed63ca91dc2cd6352f25d911f4410b0fbd59846f54206da773789d5b959c8c02bde5adf20  lizardfs-3.12.0.tar.gz
2c78af05c2eeba866c25c58d996c64d16a590248660a362e29a52065af93c126c6eb3232c8cdbb797e60cca406608b77f29ce62717446b7c3d1a8d35e0a722d8  lizardfs-cgiserv.initd
f5de527f2c6f729d49c365cbac64d938d8f90de2c78ac2f4fdbb445532fbc6438ed7380b7963f70cfbf99d48282d7da6a8dbb3df9ae84ac7d2fcebda2ce308c8  lizardfs-chunkserver.initd
6234131965bf52ef0bb859a50010b649b10b81786ff302f6ff65429d03026b32d0e1ad2daeab99dea4bd8cc94ac5aed75707d640adf33b4fb79e22185e77ac01  lizardfs-master.initd
76951ecdbaa69cef0fc383bbc3d36b1eb00aced3c98750676ab159a44a9e50d29ebe188080a08a2a09df6c62f6899bb093cf94e9e3e1688826f5ff8dfdc0fa68  lizardfs-metalogger.initd
a565d1eb682934d47bcae7e0b3ce37a08fa0e28b732e965083a878f5e9e1e846b329643c8ab02bc0f74eac54881d5287c739df6df528b78ccfc388b76af5b8c8  677.patch"
